home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / pcutils / os2 / apple / source / apple.cpp next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  4.2 KB  |  190 lines

  1. #include "easy.h"
  2.  
  3. #include "apple.h"
  4.  
  5. String title(IDS_Title);
  6. extern Thread thread;
  7.  
  8. const double XMIN=-2.2,XMAX=0.8,YMIN=-1.5,YMAX=1.5;
  9. const int ITERATIONS=100;
  10.  
  11. class MyWindow : public Window
  12. {   double Xmin,Xmax,Ymin,Ymax;
  13.     int Iterations,Active;
  14.     Rectangle R;
  15.     public :
  16.     MyWindow () : Window(ID_Window,title,FCF_NORMAL|FCF_MENU)
  17.     {    Xmin=XMIN; Xmax=XMAX; Ymin=YMIN; Ymax=YMAX;
  18.         Iterations=ITERATIONS; Active=0;
  19.         init();
  20.     }
  21.     virtual void sized ();
  22.     virtual void redraw (PS &ps);
  23.     virtual void clicked (LONG x, LONG y, clicktype click);
  24.     void draw ();
  25.     void activate () { Active=1; thread.start(); }
  26.     double xmin () { return Xmin; }
  27.     double xmax () { return Xmax; }
  28.     double ymin () { return Ymin; }
  29.     double ymax () { return Ymax; }
  30.     int iterations () { return Iterations; }
  31.     void set (double xmin, double xmax, double ymin, double ymax,
  32.         int iterations);
  33. };
  34. MyWindow window;
  35. Menu menu(window);
  36. Help help(window,ID_Help,"APPLE.HLP","");
  37.  
  38. BitmapPS *bitmap=0;
  39.  
  40. void MyWindow::set (double xmin, double xmax,
  41.     double ymin, double ymax, int iterations)
  42. {    if (xmin>=xmax) return;
  43.     Xmin=xmin; Xmax=xmax;
  44.     Ymax=(ymax+ymin)/2+(Xmax-Xmin)/2*(double)height()/width();
  45.     Ymin=(ymax+ymin)/2-(Xmax-Xmin)/2*(double)height()/width();
  46.     Iterations=iterations;
  47. }
  48.  
  49. void copy ()
  50. {    WindowPS ps(window);
  51.     if (!window.visible()) return;
  52.     bitmap->copy(ps);
  53. }
  54.  
  55. void MyWindow::draw ()
  56. {   int i,j,k,f;
  57.     double zre,zim,wre,wim,wrenew,deltax,deltay;
  58.     ULONG color;
  59.     bitmap->directcolor();
  60.     deltax=(Xmax-Xmin)/width();
  61.     deltay=(Ymax-Ymin)/height();
  62.     for (i=0; i<width(); i++)
  63.     {   zre=Xmin+i*deltax;
  64.         for (j=0; j<=height(); j++)
  65.         {   wre=zre;
  66.             wim=zim=Ymin+j*deltay;
  67.             for (k=0; k<Iterations; k++)
  68.             {    if (wim*wim+wre*wre>16) break;
  69.                 wrenew=wre*wre-wim*wim+zre;
  70.                 wim=2*wre*wim+zim;
  71.                 wre=wrenew;
  72.             }
  73.             if (k<Iterations)
  74.             {   f=255l*k/Iterations;
  75.                 color=Rgb(0,255-f,f);
  76.             }
  77.             else color=0;
  78.             bitmap->point(i,j,color);
  79.         }
  80.         if (i%10==0) copy();
  81.     }
  82.     window.update();
  83. }
  84.  
  85. int backdraw (Parameter p)
  86. {   window.draw();
  87.     return 0;
  88. }
  89.  
  90. Thread thread(backdraw);
  91.  
  92. void MyWindow::clicked (LONG x, LONG y, clicktype click)
  93. {   WindowPS ps(window);
  94.     double h;
  95.     double scale=(double)width()/height();
  96.     switch (rubberbox(x,y,click,R,5*scale,5,scale))
  97.     {   case RUBBER_DONE :
  98.             h=Xmin+R.x1()*(Xmax-Xmin)/width();
  99.             Xmax=Xmin+R.x2()*(Xmax-Xmin)/width();
  100.             Xmin=h;
  101.             h=Ymin+R.y1()*(Ymax-Ymin)/height();
  102.             Ymax=Ymin+R.y2()*(Ymax-Ymin)/height();
  103.             Ymin=h;
  104.             thread.start();
  105.             break;
  106.         case RUBBER_START :
  107.             thread.suspend();
  108.             break;
  109.         case RUBBER_CANCEL :
  110.             thread.resume();
  111.             break;
  112.     }
  113. }
  114.  
  115. void MyWindow::sized ()
  116. {    Ymax=Ymin+(Xmax-Xmin)*height()/width();
  117.     thread.stop();
  118.     if (bitmap) delete bitmap;
  119.     bitmap=new BitmapPS(*this);
  120.     if (Active) thread.start();
  121. }
  122.  
  123. void MyWindow::redraw (PS &ps)
  124. {    bitmap->copy(ps);
  125. }
  126.  
  127. void doexit (int command)
  128. {    exit(0);
  129. }
  130.  
  131. void dohelp (int command)
  132. {    help.general();
  133. }
  134.  
  135. FileSelector save(window,"*.bmp",1);
  136.  
  137. void dosave (int command)
  138. {    char *p=save.select();
  139.     if (!p) return;
  140.     bitmap->save(p);
  141. }
  142.  
  143. class ParamDialog : public Dialog
  144. {    public :
  145.     ParamDialog(Window &window, int id) : Dialog(window,id) {}
  146.     virtual int handler (int command);
  147. };
  148. ParamDialog d(window,ID_Dialog);
  149.  
  150. DoubleItem
  151.     xmin(ID_Xmin,d,window.xmin()),
  152.     xmax(ID_Xmax,d,window.xmax()),
  153.     ymin(ID_Ymin,d,window.ymin()),
  154.     ymax(ID_Ymax,d,window.ymax());
  155. SpinItem iterations(ID_Iterations,d,
  156.     window.iterations(),10,10000);
  157.  
  158.  
  159. int ParamDialog::handler (int command)
  160. {    if (command==ID_All)
  161.     {    xmin.reinit(XMIN); xmax.reinit(XMAX);
  162.         ymin.reinit(YMIN); ymax.reinit(YMAX);
  163.         iterations.reinit(ITERATIONS);
  164.         return 0;    }
  165.     return 1;
  166. }
  167.  
  168. void parameter (int command)
  169. {    xmin.set(window.xmin()); xmax.set(window.xmax());
  170.     ymin.set(window.ymin()); ymax.set(window.ymax());
  171.     iterations.set(window.iterations());
  172.     d.carryout();    if (d.result()!=DID_OK) return;
  173.     window.set(xmin,xmax,ymin,ymax,iterations);
  174.     window.update();
  175.     thread.start();
  176. }
  177.  
  178. int main (void)
  179. {   window.size(200,200);
  180.     window.top();
  181.     window.activate();
  182.     menu.add(IDM_Exit,doexit);
  183.     menu.add(IDM_Help,dohelp);
  184.     menu.add(IDM_Parameter,parameter);
  185.     menu.add(IDM_Save,dosave);
  186.     window.loop();
  187.     return 0;
  188. }
  189.  
  190.